package b2.widget.fight
{
	import b2.config.*;
	import b2.net.*;
	import b2.player.*;
	import b2.scene.fight.*;
	import b2.scene.fight.skills.*;
	import b2.util.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.text.*;
	import flash.ui.Mouse;
	import flash.utils.*;
	
	import org.flixel.*;
	
	public class WeaponSlot extends Sprite
	{
		//物品索引
		public var objectIndex:int;
		//物品基础等级
		public var basicLevel:int;
		//物品强化等级
		public var strengthenLevel:int;
		//冷却倒计时
		private var _cooldown:int;
		//是否禁用
		private var _disable:Boolean;
		//物品图标
		private var objectSprite:Sprite;
		//冷却图标
		private var cooldownIcon:MovieClip;
		//冷却圆形
		private var cdCircleBgr:Sprite;
		//冷却文字
		private var cooldownText:TextField;
		
		public function WeaponSlot(objectIndex:int, basicLevel:int, strengthenLevel:int)
		{
			super();
			this.objectIndex = objectIndex; 
			this.basicLevel = basicLevel;
			this.strengthenLevel = strengthenLevel;
			//背景框
			var ObjectItemBgr:Class = ResourceHelper.getInstance().getClassByName("ObjectItemBgr" + basicLevel);
			var objectItemBgr:Sprite = new ObjectItemBgr();
			//背景框缓存加速
			objectItemBgr.cacheAsBitmap = true;
			objectItemBgr.width = 50;
			objectItemBgr.height = 50;
			addChild(objectItemBgr);
			
			//物品图片
			var objectConfig:CommonObjConfig = Config.objectConfigs[objectIndex];
			var ObjectImage:Class = ResourceHelper.getInstance().getClassByName(objectConfig.iconImage);
			objectSprite = new ObjectImage();
			//物品图片缓存加速
			objectSprite.cacheAsBitmap = true;
			objectSprite.width = 46;
			objectSprite.height = 46;
			objectSprite.x = 2;
			objectSprite.y = 2;
			addChild(objectSprite);
			
			//冷却图标
			var CooldownIcon:Class = ResourceHelper.getInstance().getClassByName("CooldownIcon");
			cooldownIcon = new CooldownIcon();
			cooldownIcon.width = 90;
			cooldownIcon.height = 90;
			cooldownIcon.gotoAndStop(cooldownIcon.totalFrames);
			addChild(cooldownIcon);
			
			//圆形背景
			cdCircleBgr = new Sprite();
			cdCircleBgr.graphics.lineStyle(2, 0x000000);
			cdCircleBgr.graphics.beginFill(0xFFFFFF);
			cdCircleBgr.graphics.drawCircle(0, 0, 8);
			cdCircleBgr.graphics.endFill();
			cdCircleBgr.alpha = 0.5;
			cdCircleBgr.visible = false;
			cdCircleBgr.x = this.width / 2;
			cdCircleBgr.y = this.height / 2;
			addChild(cdCircleBgr);
			//冷却文字
			cooldownText = new TextField();
			cooldownText.selectable = false;
        	cooldownText.autoSize = TextFieldAutoSize.CENTER;
			cooldownText.defaultTextFormat = new TextFormat("微软雅黑", 10, 0x000000);
//			cooldownText.text = cooldown.toString();
			cooldownText.x = 22;
			cooldownText.y = 15;
			addChild(cooldownText);

			addEventListener(MouseEvent.CLICK, onMouseClick);
			addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
		}
		
		public function get cooldown():int
		{
			return _cooldown;
		}
		
		public function set cooldown(cd:int):void
		{
			var equipConfig:EquipConfig = Config.objectConfigs[objectIndex];
			_cooldown = Math.min(equipConfig.cooldown, Math.max(0, cd));
			var frameIndex:int = Math.ceil(cooldownIcon.totalFrames - cooldown * cooldownIcon.totalFrames / equipConfig.cooldown);
			cooldownIcon.gotoAndStop(frameIndex);
			if (cooldown > 0)
			{
				cooldownText.text = cooldown.toString();
				cdCircleBgr.visible = true;
			}
			else
			{
				cooldownText.text = "";
				cdCircleBgr.visible = false;
			}
		}
		
		public function get disable():Boolean
		{
			return _disable;
		}
		
		public function set disable(flag:Boolean):void
		{
			_disable = flag;
			if (flag)
			{
				this.filters = [new ColorMatrixFilter(BFont.blackWhiteFilter)];
			}
			else
			{
				this.filters = null;
			}
		}
		
		public function trigger():void
		{
			if (disable)
				return;
			if (cooldown > 0)
			{
				FightScene.getInstance().showBottomMessage("该武器尚未冷却！");
				return;
			}
			var state:PlayState = FlxG.state as PlayState;
			var me:PlayerInfo = PlayerInfoManager.getInstance().players[PlayerInfoManager.getInstance().myUserId];
			var myTank:TankSprite = state.tanks[me.campTankId];
			var usedFlag:Boolean = false;
			//能量封印
			if (myTank != null)
			{
				if (myTank.tankEffect.hasEffect(9))
				{
					FightScene.getInstance().showBottomMessage("能量已经被封印！");
					return;
				}
				else if (myTank.tankEffect.hasEffect(10))
				{
					FightScene.getInstance().showBottomMessage("时间停止状态下不受控制！");
					return;
				} 
				var equipConfig:EquipConfig = Config.objectConfigs[objectIndex];
				var quality:int = basicLevel * 5 + strengthenLevel;
				for (var i:int = 0; i < equipConfig.skills.length; i++)
				{
					var skillConfig:EnemySkillConfig = equipConfig.skills[i];
					if (skillConfig.index == 6)
					{
						
						if (skillConfig.bulletSkillIndex != 0)
						{
							myTank.specialBullet = skillConfig;
							myTank.fireBullet(skillConfig.bulletIndex, bulletApPlus, skillConfig.effectIndex, effectValue, effectDuration, myTank.direction, true);
							myTank.specialBullet = null;
						}
						else
						{
							var bulletApPlus:int = quality < skillConfig.bulletApPlus.length ? skillConfig.bulletApPlus[quality] : skillConfig.bulletApPlus[0] * quality; 
							var effectValue:int = quality < skillConfig.effectValue.length ? skillConfig.effectValue[quality] : skillConfig.effectValue[0] * quality;
							var effectDuration:int = quality < skillConfig.effectDuration.length ? skillConfig.effectDuration[quality] : skillConfig.effectDuration[0];
							myTank.fireBullet(skillConfig.bulletIndex, bulletApPlus, skillConfig.effectIndex, effectValue, effectDuration, myTank.direction, true);
						}
					}
					else
					{
						var skill:BasicSkill = myTank.skills[equipConfig.position + "_" + i];
						//释放技能
						skill.discharge(myTank.x + PlayState.TILE_WIDTH / 2, myTank.y + PlayState.TILE_WIDTH / 2, myTank.direction);
					}
				}
				cooldown = equipConfig.cooldown;
				state.updateTankStatus(myTank.tankId, -1, 0, 0, myTank.currentEp, -equipConfig.energyCost[quality], false, true);
			}
		}
		
		private function onMouseClick(event:MouseEvent):void
		{
			trigger();
		}
		
		private function onMouseOver(event:MouseEvent):void
		{
			Mouse.cursor = "button";
			if (!disable)
			{
				this.filters = [new ColorMatrixFilter(BFont.brightFilter)];
			}
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			Mouse.cursor = "auto";
			if (!disable)
			{
				this.filters = null;
			}
		}
	}
}